package com.example.demo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpUtils {
	public static final List<String> userAgents = new ArrayList<>();
	static {
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.{r_2_2} (KHTML, like Gecko) Chrome/41.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.{r_2_2} (KHTML, like Gecko) Chrome/41.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.{r_2_2} (KHTML, like Gecko) Chrome/41.0.2{r_0_3}.{r_1_2} Safari/537.{r_2_2}");
		userAgents.add("Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.{r_0_1}; rv:{r_2_2}.{r_1_1}) like Gecko");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4{r_0_3}.2{r_1_3}; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 7.0; InfoPath.3; .NET CLR 3.1.40{r_0_3}; Trident/6.0; en-IN)");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.{r_0_1})");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.{r_0_1})");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.{r_0_1})");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50{r_0_3}; WOW64)");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_{r_0_1}; Trident/6.{r_1_3})");
		userAgents.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.{r_0_2} (KHTML, like Gecko) ChromePlus/4.0.2{r_2_2}.3 Chrome/4.0.2{r_2_2}.3 Safari/5{r_3_2}.2");
		userAgents.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.{r_0_2}.3 (KHTML, like Gecko) Version/3.2.3 ChromePlus/4.0.222.3 Chrome/4.0.222.3 Safari/525.{r_2_2}.3");
		userAgents.add("Mozilla/5.0 (Windows NT 6.0; rv:2.{r_0_1}) Gecko/20100101 Firefox/4.0 Opera 12.{r_1_2}");
		userAgents.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.{r_0_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:{r_2_2}.{r_0_1}) Gecko/20100101 Firefox/40.{r_1_2}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.3; rv:{r_2_2}.{r_0_1}) Gecko/20100101 Firefox/36.{r_1_3}");
		userAgents.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.{r_0_1}) Gecko/20100101 Firefox/33.{r_1_3}");
		userAgents.add("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:{r_2_2}.{r_0_1}) Gecko/20130401 Firefox/31.{r_1_3}");
		userAgents.add("Mozilla/5.0 (Windows NT 5.1; rv:{r_2_2}.{r_0_1}) Gecko/20100101 Firefox/31.{r_1_3}");
		
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2{r_0_3}.{r_1_3} Safari/537.36 Core/1.53.3{r_2_3}.400 QQBrowser/9.6.11{r_3_3}.400");
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2{r_0_3}.{r_1_3} Safari/537.36 Core/1.53.3{r_2_3}.400 QQBrowser/9.6.12{r_3_3}.400");
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2{r_0_3}.{r_1_3} Safari/537.36 Core/1.53.3{r_2_3}.400 QQBrowser/9.6.13{r_3_3}.400");
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.2{r_0_3}.{r_1_3} Safari/537.36 Core/1.53.3{r_2_3}.400 QQBrowser/9.6.14{r_3_3}.400");
		userAgents.add("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.2{r_0_3}.{r_1_3} Safari/537.36 Core/1.53.3{r_2_3}.400 QQBrowser/9.6.15{r_3_3}.400");
		userAgents.add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.42{r_0_3}.{r_1_3} Safari/537.{r_2_3}");
	}
	
	public static String getUserAgent(){
		String str="";
		Long seed = System.currentTimeMillis();// 获得系统时间，作为生成随机数的种子
	    Random random = new Random(seed);
	    str =userAgents.get(random.nextInt(userAgents.size()));
		Pattern p = Pattern.compile("\\{(r_\\d+_\\d+)\\}");
		Matcher m = p.matcher(str);
		while (m.find()) {
			str = str.replace(m.group(0),getRandomString(Integer.parseInt(m.group(1).split("_")[2])));
		}
		return str;
	}
	
	public static String getRandomString(int length){
		String randString ="0123456789";
		Long seed = System.currentTimeMillis();// 获得系统时间，作为生成随机数的种子
	    Random random = new Random(seed);
	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < length; i++) {
	    	sb.append(String.valueOf(String.valueOf(randString.charAt(random.nextInt(randString.length())))));
	    }
	    return sb.toString();
	}
	
	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 100; i++) {
			Thread.sleep(10);
			System.out.println(HttpUtils.getUserAgent());
		}
	}
}
